package com.huawei.lynnbit.tool.cocos.config.game;

import com.fasterxml.jackson.databind.JavaType;
import com.huawei.lynnbit.tool.cocos.config.SystemConfig;
import com.huawei.lynnbit.tool.cocos.config.game.task.Task;
import com.huawei.lynnbit.tool.cocos.util.JsonUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Data
@Slf4j
public class GameConfig {
    private static final String cocos2BuildPath = "\\build\\jsb-link\\frameworks\\runtime-src\\proj.harmonyos-next";
    private static final String cocos3BuildPath = "\\native\\engine\\harmonyos-next";
    private static final String hapOutput = "\\entry\\build\\default\\outputs\\default";
    private static final String appOutput = "\\entry\\build\\default\\outputs\\default";
    private boolean needRun = true;
    private String projectPath;
    private String rawFilePath;
    private CocosVersion cocosVersion;
    private AppConfig appInfo;
    private ExportConfig exportInfo;
    private PackageConfig packageInfo;
    private List<Task> taskInfo;
    private SystemConfig systemConfig;

    public static List<GameConfig> newInstance(String filePath) throws IOException {
        String configStr = FileUtils.readFileToString(new File(filePath), Charset.forName("UTF-8"));
        JavaType javaType = JsonUtils.getCollectionType(ArrayList.class, GameConfig.class);
        return JsonUtils.toEntity(configStr, javaType);
    }

    public boolean isValid() {
        if (StringUtils.isAllBlank(projectPath, rawFilePath)) {
            log.error("projectPath or rawFilePath cannot all be blank");
            return false;
        }

        if (Objects.isNull(appInfo)) {
            log.error("appInfo cannot be null");
            return false;
        }

        if (!appInfo.isValid()) {
            return false;
        }

        if (Objects.nonNull(exportInfo)) {
            if (!exportInfo.isValid()) {
                return false;
            }
        }

        if (Objects.isNull(packageInfo)) {
            log.error("packageInfo cannot be null");
            return false;
        }

        if (!packageInfo.isValid()) {
            return false;
        }

        if (CollectionUtils.isNotEmpty(taskInfo)) {
            for (Task task : taskInfo) {
                if (!task.isValid()) {
                    return false;
                }
            }
        }

        return true;
    }

    public String getDevEcoProjectPath() {
        if (Objects.isNull(cocosVersion)) {
            return StringUtils.EMPTY;
        }

        if (StringUtils.isNotBlank(systemConfig.getCommonDevEcoProject())) {
            return systemConfig.getCommonDevEcoProject();
        }

        if (CocosVersion.COCOS2.equals(cocosVersion)) {
            return projectPath + cocos2BuildPath;
        } else if (CocosVersion.COCOS3.equals(cocosVersion)) {
            return projectPath + cocos3BuildPath;
        } else {
            return StringUtils.EMPTY;
        }
    }

    public String getBuildPath() {
        if (Objects.isNull(cocosVersion)) {
            return StringUtils.EMPTY;
        }

        if (CocosVersion.COCOS2.equals(cocosVersion)) {
            return projectPath + cocos2BuildPath;
        } else if (CocosVersion.COCOS3.equals(cocosVersion)) {
            return projectPath + cocos3BuildPath;
        } else {
            return StringUtils.EMPTY;
        }
    }

    public String getOutputPath() {
        if (PackageType.HAP.equals(packageInfo.getType())) {
            return getDevEcoProjectPath() + hapOutput;
        } else {
            return getDevEcoProjectPath() + appOutput;
        }
    }
}
